1 module hunt.templates.environment;
2 
3 import std.string;
4 import std.json;
5 import std.file;
6 
7 import hunt.application.config;
8 import hunt.templates.match;
9 import hunt.templates.rule;
10 import hunt.templates.parser;
11 import hunt.templates.renderer;
12 import hunt.templates.util;
13 import hunt.templates.ast;
14 
15 class Environment
16 {
17     string input_path;
18     string output_path;
19 
20     Parser parser;
21     Renderer renderer;
22 
23 public:
24     this()
25     {
26         auto tpl_path = Config.app.config.templates.path.value;
27         if(tpl_path.length == 0)
28             tpl_path = "./views/";
29         input_path = output_path = tpl_path;
30         parser = new Parser();
31         renderer = new Renderer();
32     }
33 
34     this(string global_path)
35     {
36         input_path = output_path = global_path;
37         parser = new Parser();
38         renderer = new Renderer();
39     }
40 
41     this(string input_path, string output_path)
42     {
43         this.input_path = input_path;
44         this.output_path = output_path;
45         parser = new Parser();
46         renderer = new Renderer();
47     }
48 
49     void set_statement(string open, string close)
50     {
51         regex_map_delimiters[Delimiter.Statement] = open ~ "\\s*(.+?)\\s*" ~ close;
52     }
53 
54     void set_line_statement(string open)
55     {
56         regex_map_delimiters[Delimiter.LineStatement] = "(?:^|\\n)" ~ open ~ " *(.+?) *(?:\\n|$)";
57     }
58 
59     void set_expression(string open, string close)
60     {
61         regex_map_delimiters[Delimiter.Expression] = open ~ "\\s*(.+?)\\s*" ~ close;
62     }
63 
64     void set_comment(string open, string close)
65     {
66         regex_map_delimiters[Delimiter.Comment] = open ~ "\\s*(.+?)\\s*" ~ close;
67     }
68 
69     void set_element_notation(ElementNotation element_notation_)
70     {
71         parser.element_notation = element_notation_;
72     }
73 
74     ASTNode parse(string input)
75     {
76         return parser.parse(input);
77     }
78 
79     ASTNode parse_template(string filename)
80     {
81         return parser.parse_template(input_path ~ filename);
82     }
83 
84     string render(string input, JSONValue data)
85     {
86         return renderer.render(parse(input), data);
87     }
88 
89     string render(ASTNode temp, JSONValue data)
90     {
91         return renderer.render(temp, data);
92     }
93 
94     string render_file(string filename, JSONValue data)
95     {
96         return renderer.render(parse_template(filename), data);
97     }
98 
99     string render_file_with_json_file(string filename, string filename_data)
100     {
101         auto data = load_json(filename_data);
102         return render_file(filename, data);
103     }
104 
105     void write(string filename, JSONValue data, string filename_out) {
106         std.file.write(output_path ~ filename_out,render_file(filename,data));
107     }
108 
109     void write(ASTNode temp, JSONValue data, string filename_out) {
110     	std.file.write(output_path ~ filename_out,render(temp,data));
111     }
112 
113     void write_with_json_file(string filename, string filename_data, string filename_out) {
114     	auto data = load_json(filename_data);
115     	write(filename, data, filename_out);
116     }
117 
118     void write_with_json_file(ASTNode temp, string filename_data, string filename_out) {
119     	auto data = load_json(filename_data);
120     	write(temp, data, filename_out);
121     }
122 
123     string load_global_file(string filename)
124     {
125         return parser.load_file(input_path ~ filename);
126     }
127 
128     JSONValue load_json(string filename)
129     {
130         return parseJSON(cast(string) std.file.read(input_path ~ filename));
131     }
132 };
133 
134 @property Environment Env(string inpath = "./views/")
135 {
136     auto tpl_path = Config.app.config.templates.path.value;
137     if(tpl_path.length == 0)
138             tpl_path = inpath;
139     return new Environment(tpl_path);
140 }
141 
142 @property Environment Env(string input, string ouput)
143 {
144     return new Environment(input, ouput);
145 }